﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NapCardOnline.Models;

namespace NapCardOnline.ViewModel
{
    public class ServiceCostDetailsViewModel
    {
        public int ServiceCostID { get; set; }
        public string ServiceCostUrl { get; set; }
        public string CostName { get; set; }
        public double? CostAmount { get; set; }
        public string ServiceName { get; set; }
        public double CommissionPercent { get; set; }
        public double? CommissionAmount { get; set; }
        public string PhoneNumber { get; set; }

        /// <summary>
        /// Gets the by service cost id and user id.
        /// </summary>
        /// <param name="serviceCostId">The service cost id.</param>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public static ServiceCostDetailsViewModel GetByServiceCostIdAndUserId(int serviceCostId, int userId)
        {

            using (var db = new NapCardOnlineEntities())
            {
                var objServiceCostDetail = new ServiceCostDetailsViewModel();
                var objServiceCost = db.ServiceCosts.FirstOrDefault(s => s.ServiceCostID == serviceCostId);
                if (objServiceCost != null)
                {
                    var objService = Models.Service.GetServiceByServiceId(objServiceCost.FK_ServiceID);
                    var objCost = Models.Cost.GetCostByCostId(objServiceCost.FK_CostID);
                    var objCommissionLevelId = Models.CommissionLevel.GetCommissionLevelIdByUserId(userId);
                    var objCommission =
                        Models.Commission.GetCommissionByServiceIdAndCommissionLevelId(objService.ServiceID,
                                                                                       objCommissionLevelId);
                    objServiceCostDetail.ServiceCostID = serviceCostId;
                    objServiceCostDetail.ServiceCostUrl = objServiceCost.ServiceCostUrl;
                    objServiceCostDetail.CostName = objCost.CostName;
                    objServiceCostDetail.CostAmount = objCost.CostPrice;
                    objServiceCostDetail.ServiceName = objService.ServiceName;
                    objServiceCostDetail.CommissionPercent = objCommission.CommissionPercent;
                    objServiceCostDetail.CommissionAmount = objCost.CostPrice * objCommission.CommissionPercent / 100;
                    return objServiceCostDetail;
                }
            }
            return null;

        }

    }
}